/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.agi.devsol.aplicacao.requisito;

import br.com.agi.devsol.aplicacao.estimativa.EditorEstimativa;
import br.com.agi.devsol.dominio.Estimativa;
import br.com.agi.devsol.dominio.Requisito;
import br.com.agi.devsol.dominio.VotacaoEstimativa;
import br.com.agi.devsol.dominio.VotacaoJaExistenteException;
import br.com.agi.devsol.ejb.votacaoestimativa.RepositorioVotacaoEstimativa;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.inject.Inject;
import javax.inject.Named;

/**
 *
 * @author rgrosz
 */

@ConversationScoped
@Named
public class ControladorRequisitoView implements Serializable  {
    
    @Inject
    private EditorEstimativa editorEstimativa;
    
    @Inject
    private EditorRequisito editorRequisito;
    
    @Inject
    private Conversation conversation;
      
    private VotacaoEstimativa votacaoEstimativa;
    
    public void setConversation(Conversation conversation){
        this.conversation = conversation;
    }
    
    public void initConversation(){
        if(!this.conversation.isTransient()){
            this.conversation.end();
        }
        this.conversation.begin();
        this.iniciarControle();
    }
    
    public void exibeMenu(){        
        this.iniciarControle();
        this.showMenu = true;
    }
    
    
    public void exibeInput(){
        this.iniciarControle();
        this.showInput = true;
    }
    
    public String incluirEstimativa(){        
        this.editorEstimativa.initConversation();
        
        Requisito r = editorRequisito.getEntidade(); 
        try {
            r.createVotacao();
        } catch (VotacaoJaExistenteException ex) {}        
        votacaoEstimativa = r.getVotacao();
        Estimativa estimativa = new Estimativa();        
        
        votacaoEstimativa.adicionarEstimativa(estimativa);
        this.editorEstimativa.editar(estimativa);        
        this.exibeInput();
        return "";
    }
    
    public String cancelarInclusaoEstimativa(){
        Estimativa estimativa = this.editorEstimativa.getEntidade();
        votacaoEstimativa.removerEstimativa(estimativa);
        this.exibeMenu();
        return "";
    }
    
    private void iniciarControle(){
        this.showInput = false;
        this.showMenu = false;
    }
    
    private boolean showMenu;    
    private boolean showInput;
    
    public boolean isShowMenu(){
        return this.showMenu;
    }
    
    public boolean isShowInput(){
        return this.showInput;
    }
}
